
如何讓 AI 可以根據情況來控制遊戲的場景
更改天氣, 下雪, 下雨, 或是打雷 ⚡️
以我正在開發的阿卡西AI遊戲為例
要實現AI自動化控制遊戲的場景
需要先跟 AI 定義好來回溝通的 Json 格式:
AI 回傳這個指令:
{
  "intent": "ChangeWeather",
  "weather": "Rain",        // 可用:Clear, Rain, Snow, Thunder
  "intensity": 0.8          // 0~1, 可選
}
執行完之後回傳這個給 AI :
{
  "status": "Success",
  "currentWeather": "Rain"
}
在 Unity 中簡易的測試程式碼:
using UnityEngine;
[System.Serializable]
public class WeatherCommand
{
    public string intent;
    public string weather;
    public float intensity;
}
public class SimpleWeatherController : MonoBehaviour
{
    public ParticleSystem rainFX;
    public ParticleSystem snowFX;
    public Light sunLight;
    public AudioSource thunderSFX;
    string currentWeather = "Clear";
    public string ApplyAICommand(string json)
    {
        WeatherCommand cmd = JsonUtility.FromJson<WeatherCommand>(json);
        if (cmd.intent == "ChangeWeather")
        {
            ChangeWeather(cmd.weather, cmd.intensity);
            return JsonUtility.ToJson(new { status = "Success", currentWeather = currentWeather });
        }
        return JsonUtility.ToJson(new { status = "UnknownCommand" });
    }
    void ChangeWeather(string type, float intensity)
    {
        // 關閉所有效果
        if (rainFX) rainFX.Stop();
        if (snowFX) snowFX.Stop();
        if (thunderSFX) thunderSFX.Stop();
        switch (type)
        {
            case "Clear":
                RenderSettings.skybox.color = Color.cyan;
                sunLight.intensity = 1.0f;
                break;
            case "Rain":
                if (rainFX) rainFX.Play();
                sunLight.intensity = 0.6f;
                break;
            case "Snow":
                if (snowFX) snowFX.Play();
                sunLight.intensity = 0.7f;
                break;
            case "Thunder":
                if (rainFX) rainFX.Play();
                sunLight.intensity = 0.5f;
                if (thunderSFX) thunderSFX.Play();
                break;
        }
        currentWeather = type;
        Debug.Log($"🌤 Weather changed to {type}");
    }
}
當 AI 回傳的 Json 經過解析之後
我們就可以執行 ChangeWeather 的功能來進行改編遊戲天氣的效果啦